package edu.scut.yi.yx.lobbymanager;

import java.util.HashMap;
import java.util.Map;
import edu.scut.yi.yx.processer.*;

public class Processers {
	private final Map<String, AbstractProcesser> processors = new HashMap<String, AbstractProcesser>();
	
	public Processers(){
		register(new AddBuddyProcesser());
		register(new LoadBuddiesProcesser());
		register(new GetUserInfoProcesser());
		register(new RemoveBuddyProcesser());
		register(new SetUserInfoProcesser());
		register(new GetGameListProcesser());
		
	}
	
	private  void register(AbstractProcesser processor){
		if(processor.getCommand() == null || processor.getCommand().length() == 0) {
            throw new IllegalStateException("Attempted to register a processor that has a null or empty command! Processor details: " + processor);
        }

        // Register the processor
        AbstractProcesser existing = processors.put( processor.getCommand(), processor );

        // If we have two processors with the same command, throw an exception
        if ( existing != null ) {
            throw new IllegalStateException(
                "Two processors registered for the '" + processor.getCommand() + "' command, " + processor + " and " +
                existing );
        }
	}
	
    public AbstractProcesser forCommand(String command){
        return processors.get(command);
    }

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString()+" new processers";
	}
    
    

}
